package mi.payment.ui.base;

import android.app.Dialog;
import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.LinearInterpolator;
import android.widget.ImageView;
import android.widget.TextView;

import mi.payment.MyApplication;
import mi.payment.R;

/**
 * 下载主密钥弹框
 * Created by lj on 2017/3/7.
 */
public class OneButtonDialog extends Dialog {
    private static ImageView loadingProgress;
    private static TextView ok, msgText;
    private static OneButtonDialog oneButtonDialog;

    public OneButtonDialog(Context context, int theme) {
        super(context, theme);
    }

    public static OneButtonDialog createOneButtonDialog(Context context) {
        oneButtonDialog = new OneButtonDialog(context, sunmi.sunmiui.R.style.defaultDialogStyle);
        oneButtonDialog.setContentView(R.layout.one_button_dialog_prompt);
        oneButtonDialog.getWindow().getAttributes().gravity = Gravity.CENTER; //居中
        oneButtonDialog.setCanceledOnTouchOutside(false); //点击空白不取消
        oneButtonDialog.setCancelable(false); //点击返回按钮不取消
        ok = (TextView) oneButtonDialog.findViewById(R.id.ok);
        loadingProgress = (ImageView) oneButtonDialog.findViewById(R.id.progress);
        msgText = (TextView) oneButtonDialog.findViewById(R.id.msg);
        loadingProgress();
        ok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                oneButtonDialog.dismiss();
            }
        });
        return oneButtonDialog;
    }

    public static void loadingProgress() {
        if (oneButtonDialog != null) {
            Animation operatingAnim = AnimationUtils.loadAnimation(oneButtonDialog.getContext(), R.anim.loading_anim);
            LinearInterpolator lin = new LinearInterpolator();
            operatingAnim.setInterpolator(lin);
            loadingProgress.setAnimation(operatingAnim);
            loadingProgress.startAnimation(operatingAnim);
        }
    }

    public void setClickAble(boolean canClick) {
        ok.setEnabled(canClick);
        if (canClick) {
            ok.setTextColor(MyApplication.getInstance().getResources().getColor(R.color.black));
            loadingProgress.clearAnimation();
            loadingProgress.setVisibility(View.GONE);
        } else {
            ok.setTextColor(MyApplication.getInstance().getResources().getColor(R.color.CEEEEEE));
        }
    }

    public void setMsg(String msg) {
        msgText.setText(msg);
    }

    public void setMsg(int resId) {
        msgText.setText(resId);
    }

}
